{% extends 'inventory/base.html' %}

{% block title %}商品管理 - {{ block.super }}{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
                    <div>
                        <h2 class="card-title mb-0">商品列表</h2>
                        <p class="text-muted mb-md-0">管理所有商品信息</p>
                    </div>
                    <div class="d-flex flex-wrap gap-2">
                        <a href="{% url 'barcode_product_create' %}" class="btn btn-success">
                            <i class="bi bi-upc-scan me-1"></i> 扫码添加商品
                        </a>
                        <a href="{% url 'product_create' %}" class="btn btn-primary">
                            <i class="bi bi-plus-circle me-1"></i> 手动添加商品
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row g-3 mb-4 align-items-center">
                    <div class="col-md-6">
                        <div class="input-group">
                            <span class="input-group-text bg-light"><i class="bi bi-search"></i></span>
                            <input type="text" class="form-control" id="searchInput" placeholder="搜索商品名称或条码...">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-md-end">
                            <div class="btn-group" id="categoryFilter">
                                <button type="button" class="btn btn-outline-primary active" data-category="">全部</button>
                                {% for category in categories %}
                                <button type="button" class="btn btn-outline-primary" data-category="{{ category.id }}">{{ category.name }}</button>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-striped table-hover align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>商品名称</th>
                                <th>类别</th>
                                <th>颜色</th>
                                <th>尺码</th>
                                <th>单价</th>
                                <th class="d-none d-md-table-cell">描述</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for product in products %}
                            <tr data-category="{{ product.category.id }}">
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="flex-shrink-0 me-3">
                                            {% if product.image %}
                                            <img src="{{ product.image.url }}" alt="{{ product.name }}" class="rounded" width="48" height="48" style="object-fit: cover;">
                                            {% else %}
                                            <div class="bg-light rounded d-flex align-items-center justify-content-center" style="width: 48px; height: 48px">
                                                <i class="bi bi-box text-secondary"></i>
                                            </div>
                                            {% endif %}
                                        </div>
                                        <div>
                                            <h6 class="mb-0">{{ product.name }}</h6>
                                            <small class="text-muted">{{ product.barcode }}</small>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-info text-dark">{{ product.category.name }}</span>
                                </td>
                                <td>
                                    {% if product.color %}
                                    <span class="badge bg-secondary d-inline-block mb-1">{{ product.get_color_display }}</span>
                                    {% else %}
                                    <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if product.size %}
                                    <span class="badge bg-dark d-inline-block mb-1">{{ product.get_size_display }}</span>
                                    {% else %}
                                    <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="fw-bold">¥{{ product.price }}</span>
                                </td>
                                <td class="d-none d-md-table-cell">{{ product.description|truncatewords:10 }}</td>
                                <td>
                                    <div class="btn-group">
                                        <a href="{% url 'product_edit' product.id %}" class="btn btn-sm btn-outline-primary" title="编辑">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <a href="#" class="btn btn-sm btn-outline-danger" title="删除">
                                            <i class="bi bi-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="7" class="text-center py-5">
                                    <div class="d-flex flex-column align-items-center">
                                        <i class="bi bi-inbox text-muted" style="font-size: 2.5rem;"></i>
                                        <p class="mt-3 mb-1">暂无商品数据</p>
                                        <small class="text-muted">点击"添加商品"按钮创建商品</small>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 搜索功能
        const searchInput = document.getElementById('searchInput');
        searchInput.addEventListener('input', filterProducts);
        
        // 分类筛选功能
        const categoryButtons = document.querySelectorAll('#categoryFilter button');
        categoryButtons.forEach(button => {
            button.addEventListener('click', function() {
                // 移除所有按钮的active类
                categoryButtons.forEach(btn => btn.classList.remove('active'));
                // 给当前点击的按钮添加active类
                this.classList.add('active');
                
                filterProducts();
            });
        });
        
        function filterProducts() {
            const searchTerm = searchInput.value.toLowerCase();
            const activeCategory = document.querySelector('#categoryFilter button.active').dataset.category;
            
            const rows = document.querySelectorAll('tbody tr');
            
            rows.forEach(row => {
                // 忽略空行
                if (!row.querySelector('td:first-child')) return;
                
                const productName = row.querySelector('h6')?.textContent.toLowerCase() || '';
                const barcode = row.querySelector('small')?.textContent.toLowerCase() || '';
                const rowCategory = row.dataset.category;
                
                // 检查搜索条件
                const matchesSearch = productName.includes(searchTerm) || barcode.includes(searchTerm);
                
                // 检查分类条件
                const matchesCategory = !activeCategory || rowCategory === activeCategory;
                
                // 同时满足搜索和分类条件才显示
                if (matchesSearch && matchesCategory) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        }
    });
</script>
{% endblock %}